package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private SetmealDishMapper setmealDishMapper;

    @Resource
    DishMapper dishMapper;

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置页码，每页记录数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //调用查询
        Page<Setmeal> page = setmealMapper.page(setmealPageQueryDTO);
        //返回数据
        return PageResult.builder()
                .total(page.getTotal())
                .records(page.getResult())
                .build();
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    //@Transactional(rollbackFor = Exception.class)
    public void add(SetmealDTO setmealDTO) {
        //添加套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.add(setmeal);

       Long id= setmeal.getId();
       //健壮性优化：苍穹业务要求：套餐必须有菜品，没有菜品数据用户端会报错
        if(setmealDTO.getSetmealDishes().size() <= 0){
            throw  new BaseException("菜品数据不能为空行");
        }
        //添加套餐菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });


        setmealDishMapper.add(setmealDishes);
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO findId(Long id) {
        //查询套餐
        Setmeal setmeal = setmealMapper.findId(id);
        //查询套餐相关的菜品
         //1.查询套餐菜品关系表，查询出本套餐说有菜品的id号
        List<Long> setmealDishIds = setmealDishMapper.findDishIds(id);
         //2.根据查询出的id号查询菜品先关信息
        List<SetmealDish> setmealDishList = dishMapper.findSetmealId(setmealDishIds);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
            setmealVO.setSetmealDishes(setmealDishList);

        return setmealVO;
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> ids) {
        //判断套餐是否在起售中，起售中的删除返回错误信息
        Integer count = setmealMapper.findIds(ids);
        if(count>0){
            throw new BaseException(MessageConstant.SETMEAL_ON_SALE);
        }
        //删除套餐
        setmealMapper.delete(ids);
        //删除相关套餐菜品关系
        setmealDishMapper.deletes(ids);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐
        setmealMapper.update(setmeal);

        //删除套餐原本的菜品
        setmealDishMapper.delete(setmealDTO.getId());

        //重新添加套餐菜品关系
        //添加套餐菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });

        setmealDishMapper.add(setmealDishes);

    }
}
